/*
 * Created by JFormDesigner on Wed Feb 01 12:58:11 CET 2012
 */

package SMSGUI;

import java.awt.Color;
import java.awt.event.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.*;

import jxl.read.biff.BiffException;

import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

/**
 * @author Erik Egmond
 */
public class GUI1 extends JPanel {
	HashMap<Integer, String[]> excelData=null;
	
	public GUI1() {
		
		initComponents();
		classify.setVisible(false);
		progressBarCorrecting.setVisible(false);
		SentimentBarMan.setVisible(false);
		labelMan.setVisible(false);
		SentimentBarVrouw.setVisible(false);
		labelVrouw.setVisible(false);
	}

	private void clickReadFile(MouseEvent e) {
		message.setBackground(Color.white);	
		message.setText("opening file...");
		//file, tweet, sentiment, name, province, city
		try {
			String [] data;
			excelData = ReadExcel.readFiveColumns(fileName.getText(), 9, 4, 11, 15, 16);
			data = excelData.get(0);
			try{
				Integer.parseInt(data[1]);
				message.setBackground(Color.green);	
				message.setText("file opened");	
				classify.setVisible(true);
			}catch(Exception e1){
				message.setBackground(Color.red);	
				message.setText("file has not the right format");	
				e1.printStackTrace();
			}	
		} catch (IOException e1) {
			message.setBackground(Color.red);
			message.setText("Error opening file...");	
		} catch ( BiffException e2){
			message.setBackground(Color.red);	
			message.setText("Could not regonize the file type");
		}
	}

	private void classifyMouseClicked(MouseEvent e) {
		message.setBackground(Color.yellow);
		message.setText("correcting tweets");
		int number;
		String [] data, nameS = new String[2];
		String tweet;
		HashMap<Integer, String> tweetMap = new HashMap<Integer, String>();
		Iterator<Integer> kExcelData = excelData.keySet().iterator();
		while (kExcelData.hasNext()){
			nameS = new String[2];
			number = kExcelData.next();
			data = excelData.get(number);
			tweet = data[0];
			tweetMap.put(number, tweet);	
		}

		
		HashMap <Integer, String> tweets = new HashMap<Integer, String>();
		
		//correct tweets
		Iterator<Integer> k = tweetMap.keySet().iterator();
		double amountOftweets = tweetMap.size(),done = 1;
		int index;
		String data1;
		while(k.hasNext()){
			index = k.next();
			System.out.println(done+"\t"+(double)(done/amountOftweets)*100);
			progressBarCorrecting.setValue((int)(done/amountOftweets)*100);
			done++;
			data1 = tweetMap.get(index);
			tweet = data1;
			System.out.println(tweet);
			System.out.println("NOT CORRECTING TWEET!!!!");
			//tweet = Functions.CorrectTweet(tweet);			
			System.out.println(tweet);
			tweets.put( index, tweet );
		}
		//end correct tweets
		
		HashMap<Integer, Integer> sentimentMap;
		try {
			sentimentMap = Functions.classify(tweets);
		
			Iterator<Integer> tweetlistIterator = excelData.keySet().iterator();
			int cur;
			while (tweetlistIterator.hasNext()){
				cur = tweetlistIterator.next();
				data = excelData.get(cur);
				System.out.println("cur "+ cur);
				System.out.println(sentimentMap.keySet());
				data[1] = "" + sentimentMap.get(cur);
				excelData.put(cur,data);
			}
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			message.setText("could not classify");
		}
		kExcelData = excelData.keySet().iterator();
		HashMap<Integer, String[]> nameAndSentiment = new HashMap<Integer, String[]>();
		while (kExcelData.hasNext()){
			nameS = new String[2];
			number = kExcelData.next();
			data = excelData.get(number);
			nameS[0] = data[2];
			nameS[1] = data[1];
			nameAndSentiment.put(number,nameS);
		}
		
		try {
			float[] sentimentList = Functions.genderFinder(nameAndSentiment);
			SentimentBarMan.setVisible(true);
			SentimentBarVrouw.setVisible(true);
			labelMan.setVisible(true);
			labelVrouw.setVisible(true);
			SentimentBarMan.setMaximum(4*(int)sentimentList[1]);
			SentimentBarMan.setValue((int)sentimentList[0]+(2*(int)sentimentList[1]));

			SentimentBarVrouw.setMaximum(4*(int)sentimentList[3]);
			SentimentBarVrouw.setValue((int)sentimentList[2]+(2*(int)sentimentList[3]));
		} catch (IOException e1) {
			message.setText("error finding gender");
		}
		
		kExcelData = excelData.keySet().iterator();
		while (kExcelData.hasNext()){
			number = kExcelData.next();
			data = excelData.get(number);
			tweet = data[0];
			tweetMap.put(number, tweet);				
		}
		
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Erik Egmond
		fileName = new JTextField();
		button1 = new JButton();
		message = new JTextField();
		classify = new JButton();
		progressBarCorrecting = new JProgressBar();
		labelMan = new JLabel();
		SentimentBarMan = new JProgressBar();
		labelVrouw = new JLabel();
		SentimentBarVrouw = new JProgressBar();
		labelTweet = new JLabel();
		label2 = new JLabel();
		button3 = new JButton();
		tweetField = new JTextField();
		sentimentField = new JTextField();
		button2 = new JButton();

		//======== this ========

		// JFormDesigner evaluation mark
		setBorder(new javax.swing.border.CompoundBorder(
			new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
				"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
				javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
				java.awt.Color.red), getBorder())); addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});

		setLayout(new FormLayout(
			"default, $lcgap, [50dlu,min], 8*($lcgap, default)",
			"9*(default, $lgap), default"));

		//---- fileName ----
		fileName.setText("GuiTest.xls");
		add(fileName, CC.xywh(3, 3, 2, 1));

		//---- button1 ----
		button1.setText("Read File");
		button1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				clickReadFile(e);
			}
		});
		add(button1, CC.xy(7, 3));
		add(message, CC.xywh(3, 5, 17, 1));

		//---- classify ----
		classify.setText("Classify");
		classify.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				classifyMouseClicked(e);
			}
		});
		add(classify, CC.xy(3, 7));
		add(progressBarCorrecting, CC.xy(3, 9));

		//---- labelMan ----
		labelMan.setText("Man");
		add(labelMan, CC.xy(1, 11));
		add(SentimentBarMan, CC.xywh(3, 11, 17, 1));

		//---- labelVrouw ----
		labelVrouw.setText("Vrouw");
		add(labelVrouw, CC.xy(1, 13));
		add(SentimentBarVrouw, CC.xywh(3, 13, 17, 1));

		//---- labelTweet ----
		labelTweet.setText("Tweet:");
		add(labelTweet, CC.xy(3, 15));

		//---- label2 ----
		label2.setText("Sentiment:");
		add(label2, CC.xy(7, 15));

		//---- button3 ----
		button3.setText("text");
		add(button3, CC.xy(1, 17));
		add(tweetField, CC.xy(3, 17));
		add(sentimentField, CC.xy(7, 17));

		//---- button2 ----
		button2.setText("Volgende");
		add(button2, CC.xy(11, 17));
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Erik Egmond
	private JTextField fileName;
	private JButton button1;
	private JTextField message;
	private JButton classify;
	private JProgressBar progressBarCorrecting;
	private JLabel labelMan;
	private JProgressBar SentimentBarMan;
	private JLabel labelVrouw;
	private JProgressBar SentimentBarVrouw;
	private JLabel labelTweet;
	private JLabel label2;
	private JButton button3;
	private JTextField tweetField;
	private JTextField sentimentField;
	private JButton button2;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
